const WXAPI = require('../../Wxapi/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    isHide: false,

    votes: '-',
    shares: '-',
    ready: false,

    loginInfo: {
      title: '微信授权',
      content: '获得您的公开信息(昵称、头像等)',
      logName: '普瑞眼科',
      logImage: '/images/wx.png',
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this,
        dialogComponent = that.selectComponent('.wxc-dialog');
    // 判断用户状态
    wx.checkSession({
      success() {
        if (wx.getStorageSync("userInfo")){
          wx.showLoading({
            title: '加载中',
            mask: true
          })
          // 查询基础展示信息
          WXAPI.getBaseMineInfo({
            uid: wx.getStorageSync('uid'),
            openId: wx.getStorageSync('openId'),
            aid: wx.getStorageSync('activeInfo').id
          }).then(function (res) {
            that.setData({
              votes: res.vote,
              shares: res.share,
              ready: true
            })
          }).finally(function(){
            wx.hideLoading()
          })
        } else {
          console.log("未登录, 显示授权页面")
          dialogComponent && dialogComponent.show();
        }
      },
      fail() {
        console.log("验证登陆信息失败, 显示授权页面")
        dialogComponent && dialogComponent.show();
      }
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },
  guize: function () {
    wx.switchTab({
      url: '/pages/guize/guize'
    })
  },
  tpjilu: function () {
    let uid = wx.getStorageSync("uid"),
        aid = wx.getStorageSync("activeInfo").id;
    wx.navigateTo({
      url: '/pages/tpjilu/tpjilu?uid='+ uid + "&aid=" + aid
    })
  },
  zjjilu: function () {
    wx.navigateTo({
      url: '/pages/zjjilu/zjjilu'
    })
  },
  goLoginPageTimeOut:function(){
    setTimeout(function () {
      wx.navigateTo({
        url: '/pages/authorize/index'
      })
    }, 1000)
  },
  onConfirm(e) { // 点击允许
    let that = this,
      dialogComponent = that.selectComponent('.wxc-dialog');
    let userInfo = e.detail.detail.rawData
    if (!userInfo) {
      return false
    } else {
      that.login()
    }
  },
  login: function () {
    let that = this;
    wx.login({
      success: function (res) {
        WXAPI.getUserInfo({
          code: res.code
        }).then(function (res) {
          if (res.code == 200) {
            console.log("用户已存在", res)
            wx.setStorageSync("uid", res.user_info.id);
            wx.setStorageSync("openId", res.user_info.openId)
            wx.getUserInfo({
              success: function (res) {
                console.log("获取用户信息并设置缓存:", res.userInfo)
                wx.setStorageSync("userInfo", res.userInfo)
              },
              complete: function () {
                let dialogComponent = that.selectComponent('.wxc-dialog');
                dialogComponent && dialogComponent.hide();
                that.onShow()
              }
            })
          } else if (res.code == 201) {
            console.log("用户注册");
            // 注册用户
            that.registerUser(res.openId)
          }
        })
      }
    })
  },
  // 注册
  registerUser: function (openId) {
    let that = this;
    console.log("开始正式注册")
    wx.login({
      success: function (res) {
        let code = res.code; // 微信登录接口返回的 code 参数，下面注册接口需要用到
        wx.getUserInfo({
          success: function (res) {
            let avatarUrl = res.userInfo.avatarUrl,
              nickName = res.userInfo.nickName,
              gender = res.userInfo.gender,
              country = res.userInfo.country,
              province = res.userInfo.province,
              city = res.userInfo.city
            // 下面开始调用注册接口
            WXAPI.register({
              avatarUrl: avatarUrl,
              nickName: nickName,
              openId: openId,
              gender: gender,
              country: country,
              province: province,
              city: city,
            }).then(function (res) {
              that.login()
            })
          }
        })
      }
    })
  }
})